import router, { asyncRoutes } from './router'
import store from './store'
// import store from './store'
// import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
// import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'

router.beforeEach(async(to, from, next) => {
  NProgress.start() // 开启进度条
  // console.log(to)
  // console.log(from)
  // console.log(next)
  // document.title = '人力资源管理系统 - ' + to.meta.title
  document.title = getPageTitle(to.meta.title)
  const whiteList = ['/login', '/404']
  const token = store.state.user.token
  if (token) {
    if (to.path === '/login') {
      next('/')
      // 如果 next 指定了跳转路径 就不会经过 router.afterEach
      NProgress.done()
      // 如果 next 直接放行 才会经过 router.afterEach
    } else {
      // 获取用户信息 存入到vuex中
      // 如果vuex中有数据了，跳转页面就不用再获取数据了
      if (!store.state.user.userInfo.userId) {
        const menus = await store.dispatch('user/getUserInfo')
        // console.log(menus)
        // console.log(asyncRoutes)
        // 筛选可以访问的路由
        const fliterRoutes = asyncRoutes.filter(item => menus.includes(item.children[0].name))
        fliterRoutes.push({ path: '*', redirect: '/404', hidden: true })
        router.addRoutes(fliterRoutes)
        // 将动态路由添加到 vuex 中
        store.commit('menus/setMenus', fliterRoutes)
        // 使用next({ ...to,replace: true})来确保addRoutes()时动态添加的路由已经被完全加载上去
        // 解决刷新白屏
        next({ ...to, replace: true })
      } else {
        next()
      }
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})

// 页面跳转完成会经过这个钩子函数
router.afterEach(() => {
  NProgress.done() // 关闭进度条
})

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

// const whiteList = ['/login'] // no redirect whitelist

// router.beforeEach(async(to, from, next) => {
//   // start progress bar
//   NProgress.start()

//   // set page title
//   document.title = getPageTitle(to.meta.title)

//   // determine whether the user has logged in
//   const hasToken = getToken()

//   if (hasToken) {
//     if (to.path === '/login') {
//       // if is logged in, redirect to the home page
//       next({ path: '/' })
//       NProgress.done()
//     } else {
//       const hasGetUserInfo = store.getters.name
//       if (hasGetUserInfo) {
//         next()
//       } else {
//         try {
//           // get user info
//           await store.dispatch('user/getInfo')

//           next()
//         } catch (error) {
//           // remove token and go to login page to re-login
//           await store.dispatch('user/resetToken')
//           Message.error(error || 'Has Error')
//           next(`/login?redirect=${to.path}`)
//           NProgress.done()
//         }
//       }
//     }
//   } else {
//     /* has no token*/

//     if (whiteList.indexOf(to.path) !== -1) {
//       // in the free login whitelist, go directly
//       next()
//     } else {
//       // other pages that do not have permission to access are redirected to the login page.
//       next(`/login?redirect=${to.path}`)
//       NProgress.done()
//     }
//   }
// })

// router.afterEach(() => {
//   // finish progress bar
//   NProgress.done()
// })
